/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helpdesk;
    
import java.util.UUID;
import java.util.ArrayList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
 *
 * @author John
 */
public class Post {
    
    String author = "";
    String title = "";
    String body = "";
    String postId;// = UUID.randomUUID().toString();
    int roleIndex;
    String responseId = null;
    ArrayList<String> responses = null;
    
    public Post ()
    {
        
    }
    
    public Post (String author, String title, String body, String postId, String responseId, ArrayList<String> responseList, int roleIndex)
    {
        this.author = author;
        this.title = title;
        this.body = body;
        this.roleIndex = roleIndex;
        this.responses = responseList;
        if (postId != null)
        {
            this.postId = postId;
        }
        else
        {
            this.postId = generateId();
        }
        this.responseId = responseId;
    }
    
    private String getPostId()
    {
        return this.postId;
    }
    
    public BasicDBObject toBasicDBObject()
    {
        BasicDBObject mappedObj = new BasicDBObject();
        mappedObj.append("author", author);
        mappedObj.append("title", title);
        mappedObj.append("body", body);
        mappedObj.append("postId", postId);
        mappedObj.append("responseId", responseId);
        mappedObj.append("roleIndex", roleIndex);
        ArrayList<UUID> responses = new ArrayList<>();  
        mappedObj.append("responses", responses);
        
        return mappedObj;
    }
    
    public static Post toPost(DBObject obj)
    {
        return new Post( obj.get("author").toString(), obj.get("title").toString(), obj.get("body").toString(), obj.get("postId").toString(), obj.get("responseId").toString(), (ArrayList<String>) obj.get("responses"), (int) obj.get("roleIndex"));
    }
    
    public static String generateId()
    {
        return UUID.randomUUID().toString();
    }
    
    public static boolean deletePost(String postId)
    {
        return true;
    }
    
    public void addResponse(String response)
    {
        this.responses.add(response);
    }
}
